<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //扫描抢输入事件,监听到扫描枪输入后会调用
        function onScanInput(str) {

        }

        window.onload = function (e) {
            var codeString = "";
            //定时器每隔200ms 清空codeString
            var scanTimeer = setInterval(function () {
                var now = new Date().getTime();
                if (now - lastTime > 200) {
                    codeString = "";
                }
            },
                200);

            var lastTime;
            var caseFormat = false;
            document.onkeydown = function (e) {
                var nextTime = new Date().getTime();
                console.info(e.code+"--"+e.key);
                var code = e.which;
                //shift
                if (code == 16) {
                    caseFormat = true;
                } else {
                    if (caseFormat) {
                        if (code >= 65 && code <= 90) {
                            //转小写
                            code = code + 32;
                        } else if (code >= 97 && code <= 122) {
                            //转大写
                            code = code - 32;
                        }
                        caseFormat = false;
                    }
                    var char = String.fromCharCode(code);
                    

                    if (codeString == "") {
                        codeString += char;
                    } else if (nextTime - lastTime <= 30) {
                        codeString += char;
                    }
                }
                //console.log(codeString);
                lastTime = nextTime;
                if (e.which == 13) {
                    console.log("qr:" + codeString);
                    codeString = ""; //回车输入后清空
                }
            };

        };

    </script>

</body>

</html>